<?php
class NewsAction extends CommonAction {
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('News');
		$this->modelViewObject = D('NewsView');
		$this->modelContentObject = D('NewsContent');
		
		$this->assign('cateOption', R('NewsCate/getCateOption'));
    }
	
    public function index(){
		$thisPage = $this->pageInfo['p'] = I('p', 1);
		
		if($this->pageInfo['name'] = I('name', '')){
			$map['name'] = array('LIKE', '%'.$this->pageInfo['name'].'%');
		}
		
		if($this->pageInfo['status'] = I('status', '')){
			$map['status'] = $this->pageInfo['status'];
		}
		
		if($this->pageInfo['cate_id'] = I('cate_id', '')){
			$map['cate_id'] = $this->pageInfo['cate_id'];
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('pageInfo', $this->pageInfo);
		$this->assign('pageInfo', $this->pageInfo);
		$this->assign('list', $list);
		
		$this->assign('categoryTextArray', D('NewsCate')->getAllCateTextArray());
		
		$this->display();
    }
	
	public function add(){
		if(IS_POST){
			$cateId = I('cate_id', 0, 'intval');
			if($cateId == 0){
				$this->error('请选择分类!');	
			}
			
			//获取附表内容
			$contentData = array();
			$contentData['title'] = I('title', '');
			$contentData['keywords'] = I('keywords', '');
			$contentData['description'] = I('description', '');
			$contentData['content'] = I('content', '');
			
			/*$tempPic = I('temp_pic');
			if(empty($tempPic)){
				$savePath = C('TMPL_PARSE_STRING.__UPLOADIMAGE__');
				$uploadInfo = upload('pic', $savePath);
				if($uploadInfo){
					if($uploadInfo['code']){
						$_POST['pic'] = $uploadInfo['message'][0]['savename'];
					}else{
						$this->error($uploadInfo['message']);
					}
				}
			}else{
				$_POST['pic'] = $tempPic;
			}*/
			
			
			$_POST['sub_content'] = getSubContent($contentData['content']);
			$_POST['add_time'] = NOW_TIME;
			if($this->modelObject->create()){
				if($insertId = $this->modelObject->add()){
					$contentData['news_id'] = $insertId;
					if($this->modelContentObject->add($contentData)){				
						$this->success('插入数据成功', U('index'));
					}else{
						$this->modelObject->deleteInfo($insertId);
						$this->error('数据插入附表失败');
					}
				}else{
					$this->error('插入失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$this->display();	
		}
	}
	
	public function edit(){
		if(IS_POST){
			$cateId  = I('cate_id', 0, 'intval');
			$newsId  = I('id', 0, 'intval');
			
			if($cateId == 0){
				$this->error('请选择分类!');	
			}
							
			$tempPic = I('temp_pic');
			$pic     = I('pic');
			if($tempPic != $pic){
				delImage($tempPic);	
			}
			
			//获取附表内容
			$contentData = array();
			$contentData['title']       = I('title', '');
			$contentData['keywords']    = I('keywords', '');
			$contentData['description'] = I('description', '');
			$contentData['content']     = I('content', '');
			$contentData['update_time'] = $_POST['update_time'] = NOW_TIME;
			
			/*$savePath = C('TMPL_PARSE_STRING.__UPLOADIMAGE__');
			$uploadInfo = upload('pic', $savePath);
			if($uploadInfo){
				if($uploadInfo['code']){
					$temp_pic = I('temp_pic');
					if(!empty($temp_pic)){
						@unlink('.'.$savePath.$temp_pic);
					}
					$_POST['pic'] = $uploadInfo['message'][0]['savename'];
				}else{
					$this->error($uploadInfo['message']);
				}
			}*/
			
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					if($this->modelContentObject->where('news_id = '.$newsId)->save($contentData)){				
						$this->success('编辑成功', U('index', session('pageInfo')));	
					}else{
						$this->error('附表数据编辑失败');
					}
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$id = I('id', 0, 'intval');
			if($id){
				$info = $this->modelViewObject->getInfo($id);
				$this->assign('info', $info);
				$this->display();	
			}else{
				$this->error('参数错误!');
			}
		}
	}
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			$info = $this->modelViewObject->getInfo($id);
			if($this->modelObject->deleteInfo($id)){
				//删除附表信息
				$this->modelContentObject->deleteInfo(array('news_id'=>$id));
				delImage($info['pic']);	
				delEditorImage($info['content']);
				
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function delAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			$infoList = $this->modelViewObject->getAll(array('id'=>array('IN', $idStr)));
			if($this->modelObject->deleteInfo($idStr)){
				//删除附表信息
				$this->modelContentObject->deleteInfo(array('news_id'=>array('IN', $idStr)));
				foreach($infoList as $info){
					delImage($info['pic']);	
					delEditorImage($info['content']);	
				}
				
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
}//end class